<{include file="header.html"}>

<script type="text/javascript">
    var lang_arr = [];
    var first_element = '<{$smarty.const._LB_CHOOSE|utf8_escape_html}>';
    var err = '<{$smarty.const._MSG_COUNTRY_LANGUAGE_CODE_INVALID|utf8_escape_js}>';
    var err_1 = '<{$smarty.const._MSG_VALUE_NAME_BY_LANGUAE_INVALID|utf8_escape_js}>';
    var err_2 = '<{$smarty.const._MSG_VALUE_NAME_BY_LANGUAGE_EXISTS|utf8_escape_js}>';
    
	function validate()
	{
        var image = document.getElementById('image');
		var id = document.getElementById('ID');
        var position = document.getElementById('position');
        
        var agr = new Array();
        agr.push(id.value);
        if (!validateLang('category', 'Category::isExistsByName', agr)) {
            return false;
        }
        
        if (isNaN(position.value) || (position.value != '0' && parseInt(position.value) != position.value) || position.value < 0) 
		{
			showErrorBubble(position, '<{$smarty.const._MSG_POSITION_INVALID|utf8_escape_js}>');
			return false;
		}
                
        var agr = new Array();
		agr.push(trim(position.value));

		if (id.value)
			agr.push(id.value);
		else
			agr.push(0);
		if(callFunction('Category::isExistsByPosition',agr) )
		{
			showErrorBubble(position, '<{$smarty.const._MSG_EMPLACEMENT_POSITION_EXISTS|utf8_escape_js}>');
			return false;
		}
        
		var ext = getFileExtension(image);
		if (image.value && ext.toLowerCase() != '.pjpeg' && ext.toLowerCase() != '.jpeg' 
			&& ext.toLowerCase() != '.jpg' && ext.toLowerCase() != '.png'
			&& ext.toLowerCase() != '.gif') {
			showErrorBubble(image, '<{$smarty.const._MSG_INVALID_PICTURE|utf8_escape_js}>');
			return false;
		}
		
		document.category.submit();
	}

	function getFileExtension(image)
	{
		if(!image ) return "";
		var filename = image.value;
		if( filename.length == 0 ) return "";
		var dot = filename.lastIndexOf(".");
		if( dot == -1 ) return "";
		var extension = filename.substr(dot,filename.length);
		return extension;
	}
	
    <{foreach from=$name_arr key=code item=name}>
        lang_arr.push('<{$code}>');
    <{/foreach}>

</script>

	<div class="one_center">
		<div class="box_two_third">
			<div class="box_head"><{$smarty.const._LB_CATEGORY|utf8_escape_html}></div>
			<div class="box_content" >
				<form action="" name="category" method="post" enctype="multipart/form-data">
				<input type="hidden" id="ID" name="ID" value="<{$category->ID}>"/>
				<input type="hidden" id="act" name="act" value="<{$smarty.const.ACT_VALID_EDIT}>"/>
				<table cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td width="20%"><{$smarty.const._LB_LANGUAGE|utf8_escape_html}>&nbsp;:&nbsp;</td>
                        <td>
                            <select id="category_language" name="category_language" style="width: 180px;">
                                <option value=""><{$smarty.const._LB_CHOOSE|utf8_escape_html}></option>
                                <{foreach from=$front_language item=name key=code}>
                                   <{if $code != 'en' && !$name_arr.$code}>
                                   <option value="<{$code|utf8_escape_html}>"><{$name|utf8_escape_html}>&nbsp;(<{$code|utf8_escape_html}>)</option>'
                                   <{/if}>
                                   <script type="text/javascript">
                                      front_language_arr.push(new Front_Language("<{$code|utf8_escape_html}>", "<{$name|utf8_escape_html}>"));
                                   </script>
                                <{/foreach}>
                            </select>&nbsp;
                            <a id="add_more" onclick="addLang('category', lang_arr, 'en')" href="#">
                                <img src="<{$smarty.const.APPLICATION_URL}>/images/<{$smarty.const.IMAGE_NAME_MORE}>" title="<{$smarty.const._LB_ADD_MORE|utf8_escape_html}>" alt="<{$smarty.const._LB_ADD_MORE|utf8_escape_html}>"/>
                            </a>
                        </td>
                    </tr>
					<tr>
                        <td width="20%" valign="top"><label for="name"><{$smarty.const._LB_CATEGORY_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></label></td>
						<td>
                            <div>
                               <table>
                                   <{foreach from=$front_language item=name key=code name=name_index}>
                                        <tr id="category_div_<{$code}>" <{if $code != 'en' && !$name_arr.$code}>style="display:none"<{/if}>>
                                            <td width="10px" id="category_lbl_<{$code|utf8_escape_html}>"><{$name|utf8_escape_html}>&nbsp;(<{$code|utf8_escape_html}>)</td>
                                            <td><input <{if $code != 'en' && !$name_arr.$code}>disabled="true"<{/if}> name="name[<{$code|utf8_escape_html}>]" id="category_name_<{$code|utf8_escape_html}>" type="text" size="<{$smarty.const.SIZE_NAME}>" value="<{$name_arr.$code}>"/>
                                            <{if $code != 'en'}>
                                            <a onclick="deleteLang('category', '<{$code|utf8_escape_html}>', lang_arr, 'en')" href="#">
                                                <img src="<{$smarty.const.APPLICATION_URL}>/images/<{$smarty.const.IMAGE_NAME_MINUS_SMALL}>" title="<{$smarty.const._LB_REMOVE|utf8_escape_html}>" alt="<{$smarty.const._LB_REMOVE|utf8_escape_html}>"/>
                                            </a>
                                            <{/if}>
                                            <td>
                                        </tr>
                                    <{/foreach}>
                                </table> 
                            </div>
                        </td>
					</tr>
					<tr>
						<td width="20%"><label for="position"><{$smarty.const._LB_SEGMENT_POSITION|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></label></td>
						<td><input name="position" id="position" type="text" size="<{$smarty.const.SIZE_NAME}>" value="<{$category->POSITION|utf8_escape_html}>"/></td>
					</tr>
					<{if $category->ID && file_exists($smarty.const.CATEGORY_IMG_DIR|cat:"/"|cat:$category->PICTURE)}>
					<tr>
						<td></td>
						<td>
							<{$output->displayResizeImage($smarty.const.CATEGORY_IMG_URL, $category->PICTURE, $smarty.const.IMAGE_MAX_WIDTH_DISPLAY, $smarty.const.IMAGE_MAX_HEIGHT_DISPLAY, $smarty.const.CATEGORY_IMG_DIR)}>
						</td>
					</tr>
					<{/if}>
					<tr>
						<td width="20%"><label for="lon"><{$smarty.const._LB_CATEGORY_PICTURE|utf8_escape_html}>&nbsp;:&nbsp;</label></td>
						<td><input name="image" id="image" type="file" size="<{$smarty.const.SIZE_NAME}>" value="<{$category->PICTURE|utf8_escape_html}>"/></td>
					</tr>
                    <tr>
						<td></td>
						<td class="hint"><{$smarty.const._MSG_IMAGE_CATEGORY_HINTS|utf8_escape_html}></td>
					</tr>
                    <tr>
					  	<td class="submit" colspan="2" style="padding-top: 10px;">
							  <a id="auto_category_return" class="tribullet" id="btnReturn" href="<{$backlink}>"><{$smarty.const._LB_BACK}></a>
							  <a id="auto_category_validate" class="tribullet" href="#" onclick="validate()"><{$smarty.const._LB_VALIDATE}></a>
						</td>
					</tr>
				</table>
				</form>
			</div>
		</div>
	</div>

<{include file="footer.html"}>
